home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / sq0928.zip / UNDOS.C < prev    next >
C/C++ Source or Header  |  1991-04-27  |  6KB  |  270 lines

  1. /*% cc -xenix -M0 -compat -Osa -K -i % -o undos
  2.  *
  3.  * Undos - change DOS format files to Unix, etc.
  4.  */
  5. char ID[] =
  6.   "Undos Rev 04-25-91 Copyright Omen Technology Inc All Rights Reserved\n";
  7. /*
  8.  * This program and documentation may be copied, used, or modified
  9.  *  by ZCOMM, Professional-YAM and PowerCom licensees provided these notices
  10.  *  are not removed.  Others may use this program for non-profit purposes only.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <utime.h>
  17.  
  18. #define LL 10240
  19. #define SUB 032
  20.  
  21.  
  22. #if 0
  23. struct    utimbuf       {
  24.     time_t       actime;    /* access time */
  25.     time_t       modtime;    /* modification time    */
  26. };
  27. #endif
  28.  
  29. char Lbuf[LL+2];
  30. char *Progname;
  31. int Todos = 0;
  32. int Tocpm = 0;
  33. int Tomac = 0;
  34. int Unmac = 0;
  35. int Strip = 0;
  36. int Stripsp = 0;
  37. int Graphics = 0;
  38. int Unparity = 0;
  39. int Munged = 0;
  40. int Lineflush = 0;    /* Flush output at end of each line */
  41.  
  42. main(argc, argv)
  43. char **argv;
  44. {
  45.     Progname = *argv;
  46.     if (! strcmp(Progname, "tocpm"))
  47.         Todos = Tocpm = 1;
  48.     if (! strcmp(Progname, "todos"))
  49.         Todos = 1;
  50.     if (! strcmp(Progname, "unmac"))
  51.         Unmac = 1;
  52.     if (! strcmp(Progname, "tomac"))
  53.         Tomac = 1;
  54.     if (! strcmp(Progname, "unparity"))
  55.         Unparity = 1;
  56.  
  57.     if (! strcmp(argv[1], "-p")) {
  58.         ++Strip;  ++Stripsp; --argc; ++argv;
  59.     }
  60.     if (! strcmp(argv[1], "-s")) {
  61.         ++Strip; --argc; ++argv;
  62.     }
  63.     if (! strcmp(argv[1], "-g")) {
  64.         Strip = 0;  ++Graphics; --argc; ++argv;
  65.     }
  66.  
  67.  
  68.     if (argc == 1) {
  69.         chngfmt(NULL);  exit(0);
  70.     }
  71.  
  72.     if (argc<2 || *argv[1]== '-')
  73.         usage();
  74.     while (--argc >= 1)
  75.         chngfmt(*++argv);
  76.     exit(Munged);
  77. }
  78. usage()
  79. {
  80.     fprintf(stderr, ID);
  81.     fprintf(stderr, "\nUsage: {undos|tounix|todos|tocpm|unmac|tomac} [-p] [-s] [file ...]\n");
  82.     fprintf(stderr, "    -p Strip trailing spaces, parity bit, ignore bytes < 007\n");
  83.     fprintf(stderr, "    -s Strip parity bit, ignore bytes < 007\n");
  84.     fprintf(stderr, "    -g Allow Graphics (line drawing) characters\n");
  85.     fprintf(stderr, "-or-    unparity [file ...]\n");
  86.     exit(1);
  87. }
  88.  
  89.  
  90. chngfmt(name)
  91. char *name;
  92. {
  93.     register c;
  94.     register char *p;
  95.     register n;
  96.     register FILE *fin;
  97.     FILE *fout;
  98.     int linno = 0;
  99.     long fpos;
  100.     struct stat st, ost;
  101.     struct utimbuf times;
  102.     char outnam[64];
  103.     long ftell();
  104.     char *mktemp();
  105.     int nlong = LL;
  106.  
  107.     if (name) {
  108.         if (stat(name, &st)) {
  109.             xperror(name); return;
  110.         }
  111.         if ((st.st_mode & S_IFMT) != S_IFREG) {
  112.             fprintf(stderr, "%s: %s is not a regular file\n", Progname, name);
  113.             return;
  114.         }
  115.         if ((fin = fopen(name, "r")) == NULL) {
  116.             xperror(name); return;
  117.         }
  118.         strcpy(outnam, "undosXXXXXX");
  119.         mktemp(outnam);
  120.         if ((fout = fopen(outnam, "w")) == NULL) {
  121.             xperror(outnam); exit(2);
  122.         }
  123.     } else {
  124.         fin = stdin; fout = stdout;
  125.     }
  126.     if (fstat(fileno(fout), &ost)) {
  127.         xperror("Can't fstat output!"); return;
  128.     }
  129.     if ((ost.st_mode & S_IFMT) != S_IFREG) {
  130.         Lineflush = 1;
  131.     }
  132.  
  133.     if (Unparity) {
  134.         while ((c = getc(fin)) != EOF)
  135.             if (putc((c & 0177), fout) == EOF) {
  136.                 xperror(outnam); exit(2);
  137.             }
  138.         goto closeit;
  139.     }
  140.     for (;;) {
  141.         ++linno;
  142.         Lbuf[0] = 0;
  143.         for (p=Lbuf+1, n=LL; --n>0; ) {
  144. ignore:
  145.             if ((c = getc(fin)) == EOF)
  146.                 break;
  147.             if ( !c)
  148.                 goto ignore;
  149.             if (c & 0200 && !Graphics) {
  150.                 if (Strip) {
  151.                     if ((c &= 0177) < 7)
  152.                         goto ignore;
  153.                 } else if (name)
  154.                     goto thisbin; 
  155.             }
  156.             if (c < '\7') {
  157.                 if (Strip) {
  158.                     if ((c &= 0177) < 7)
  159.                         goto ignore;
  160.                 } else if (name)
  161.                     goto thisbin; 
  162.             }
  163.             if (c == SUB) {
  164.                 if (linno == 1 && name)    /* ARC or ZOO file */
  165.                     goto thisbin;
  166.                 break;
  167.             }
  168.             if (c == '\r' && Unmac)
  169.                 c = '\n';
  170.             *p++ = c;
  171.             if (c == '\n')
  172.                 break;
  173.         }
  174.         *p = '\0';
  175.         if (n < nlong)
  176.             nlong = n;
  177.  
  178.         if (n == 0 && name) {
  179. thisbin:
  180.             if (n) {
  181.                 fprintf(stderr, "%s: %s is a binary file", Progname, name);
  182.                 fprintf(stderr, " line=%d char =%2X\n", linno, c);
  183.             } else {
  184.                 fprintf(stderr, "line=%d char =%2X\n", linno, c);
  185.                 fprintf(stderr, "%s: %s has long line!\n", Progname, name);
  186.                 if (!Unmac)
  187.                     fprintf(stderr, "Try unmac?\n");
  188.             }
  189.             Munged = 1;  fclose(fin);  fclose(fout);
  190.             unlink(outnam);  return;
  191.         }
  192.  
  193.         if (Todos) {
  194.             if (*--p == '\n' && p[-1] != '\r') {
  195.                 *p++ = '\r'; *p++ = '\n'; *p = 0;
  196.             }
  197.         } else if (Tomac) {
  198.             if (*--p == '\n') {
  199.                 if (p[-1] == '\r')
  200.                     --p;
  201.                 *p++ = '\r'; *p = 0;
  202.             }
  203.         } else {
  204.             if (*--p == '\n' && *--p == '\r') {
  205.                 while (p>(Lbuf+1) && p[-1] == '\r')
  206.                     --p;
  207.                 if (Stripsp)
  208.                     while (p>(Lbuf+1) && p[-1] == ' ')
  209.                         --p;
  210.                 *p++ = '\n'; *p = 0;
  211.             }
  212.         }
  213.         if (Lbuf[1] && fputs(Lbuf+1, fout) == EOF) {
  214.             xperror(outnam); exit(2);
  215.         }
  216.         switch (c) {
  217.         case EOF:
  218.             if (ferror(fin)) {
  219.                 xperror(name); exit(3);
  220.             }
  221.         case SUB:
  222.             if (Tocpm) {
  223.                 fpos = ftell(fout);
  224.                 do {
  225.                     putc(SUB, fout);
  226.                 } while (++fpos & 127);
  227.             }
  228. closeit:
  229.             if ( !name)
  230.                 return;
  231.             fclose(fout); fclose(fin);
  232.             if (st.st_nlink > 1) 
  233.                 sprintf(Lbuf, "cp %s %s", outnam, name);
  234.             else
  235.                 sprintf(Lbuf, "mv %s %s", outnam, name);
  236.             system(Lbuf);
  237.             times.actime = st.st_atime;
  238.             times.modtime = st.st_mtime;
  239.             if (utime(name, ×)) {
  240.                 xperror("Can't set file date");
  241.             }
  242.             if (st.st_nlink > 1) 
  243.                 unlink(outnam);
  244.             nlong = LL - nlong;
  245.             if (nlong > 132)
  246.                 fprintf(stderr, "Longest line in %s has %d bytes.\n",
  247.                   name ? name:"stdin", nlong);
  248.             return;
  249.         }
  250.         if (Lineflush)
  251.             fflush(fout);
  252.     }
  253. }
  254.  
  255. xperror(s)
  256. char *s;
  257. {
  258.     register char *p;
  259.     extern int sys_nerr;
  260.     extern char *sys_errlist[];
  261.     extern errno;
  262.  
  263.     if (errno >= sys_nerr)
  264.         p = "Gloryovsky: a New Error!";
  265.     else
  266.         p = sys_errlist[errno];
  267.     fprintf(stderr, "%s: %s: %s\n", Progname, s, p);
  268. }
  269.  
  270.